/**
 * Created by chengcong on 2017/11/20.
 */
$('.form_datetime').datetimepicker({
	//language:  'fr',
	weekStart: 1,
	todayBtn: 1,
	autoclose: 1,
	todayHighlight: 1,
	startView: 2,
	forceParse: 0,
	showMeridian: 1
});
// const sokceturl = "http://47.90.123.205:8000";
var arr=location.search.split("=");
$(function() {
	var msg = window.sessionStorage['user_msg'];
	msg = JSON.parse(msg);
	console.log(msg);
	socket.emit('login', {
        token:msg.token,
		username: msg.username,
		openid: null,
		sex: 0,
		marking: 1, //1pc  2wx  3app  Number
		userrole: msg.userrole //登陆角色 (医生(1)，用户(2)，助理(3)，药师(4),匿名用户(5))
	});
    if(arr[1]){
        $.ajax({
            url: url + '/server/assDocFinish',
            type: 'post',
            data: {
                id: arr[1],
                userRole: 6,
                limit:10,
                page:1
            },
            headers: {
                'Authorization': 'Bearer ' + msg.token
            },
            success: function(data) {
                console.log(data);
                if(data.code == 200) {
                    $('table tbody').html('');
                    if(data.data.length > 0) {
                        var n=Math.ceil(data.count/10);
                        if(n>5){
                            var html='<li><a href="#" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>';
                            html+='<li class="active"><a href="#">1</a></li>' +
                                '<li><a href="#">2</a></li><li><a href="#">3</a></li>' +
                                '<li><a href="#">4</a></li><li><a href="#">5</a></li>';
                            html+='<li><a href="#" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>';
                            $('.pagination').html(html);
                        }else{
                            var html='';
                            for(var a=0;a<n;a++){
                                if(a==0){
                                    html+='<li class="active"><a href="#">'+(a+1)+'</a></li>'
                                }else{
                                    html+='<li><a href="#">'+(a+1)+'</a></li>'
                                }

                            }
                            html+='';
                            $('.pagination').html(html);
                        }
                        var finishlist = [];
                        data.data = data.data.sort(compare("assTime"));
                        for(var k = 0; k < data.data.length; k++) {
                            finishlist.unshift(data.data[k]);
                        }
                        console.log(finishlist);
                        for(var i = 0; i < finishlist.length; i++) {
                            $('table tbody').append('<tr><td>' + finishlist[i].name + '</td><td>' + finishlist[i].assistantName||finishlist[i].serverName + '</td>' +
                                '<td>' + finishlist[i].serTime + '</td><td>已处理</td><td>' +(msg.userrole==1?finishlist[i].docTime:finishlist[i].assTime) + '</td>' +
                                '<td><button class="btn detailbtn" listid="' + finishlist[i]._id + '" status="1">详情</button></td></tr>');
                        }
                        $('.pagination a').on('click',function (e) {
                            console.log($(e.target).text());
                            if($(e.target).text()=='下一页'){
                                if($('.pagination li.active a').text()==n ){
                                    alert('已经到头了');
                                }else if(parseInt($('.pagination li:nth-child(6) a').text())==n){
                                    $('li.active').next().addClass('active');
                                    $($('li.active')[1]).prev().removeClass('active');
                                }else{
                                    $('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text())+1);
                                    $('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text())+1);
                                    $('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text())+1);
                                    $('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text())+1);
                                    $('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text())+1);
                                }
                                $.ajax({
                                    url: url + '/server/assDocFinish',
                                    type: 'post',
                                    data: {
                                        id: arr[1],
                                        userRole: 6,
                                        limit:10,
                                        page:$('.pagination li.active a').text()
                                    },
                                    headers: {
                                        'Authorization': 'Bearer ' + msg.token
                                    },
                                    success: function (data2) {
                                        console.log(data2);
                                        if (data2.code == 200) {
                                            $('table tbody').html('');
                                            if (data2.data.length > 0) {
                                                var finishlist2 = [];
                                                data2.data = data2.data.sort(compare("docTime"));
                                                for(var k = 0; k < data2.data.length; k++) {
                                                    finishlist2.unshift(data2.data[k]);
                                                }
                                                console.log(finishlist2);
                                                for(var i = 0; i < finishlist2.length; i++) {
                                                    $('table tbody').append('<tr><td>' + finishlist2[i].name + '</td><td>' + finishlist2[i].serverName + '</td>' +
                                                        '<td>' + finishlist2[i].serTime + '</td><td>已处理</td><td>' + (msg.userrole==1?finishlist2[i].docTime:finishlist2[i].assTime) + '</td>' +
                                                        '<td><button class="btn detailbtn" listid="' + finishlist2[i]._id + '" status="1">详情</button></td></tr>');
                                                }
                                                $('.detailbtn').on('click', function(e) {
                                                    console.log(1111);
                                                    self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&'+arr[1]+'=' + $(e.target).attr('status');
                                                });
                                            }
                                        }
                                    }
                                });
                            }else if($(e.target).text()=='上一页'){
                                if($('l.pagination li.active a').text()==1 ){
                                    alert('已经到头了');
                                }else if(parseInt($('.pagination li:nth-child(2) a').text())==1){
                                    $('li.active').prev().addClass('active');
                                    $($('li.active')[0]).next().removeClass('active');
                                }else{
                                    $('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text())-1);
                                    $('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text())-1);
                                    $('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text())-1);
                                    $('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text())-1);
                                    $('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text())-1);
                                }
                                $.ajax({
                                    url: url + '/server/assDocFinish',
                                    type: 'post',
                                    data: {
                                        id: arr[1],
                                        userRole: 6,
                                        limit:10,
                                        page:$('.pagination li.active a').text()
                                    },
                                    headers: {
                                        'Authorization': 'Bearer ' + msg.token
                                    },
                                    success: function (data3) {
                                        console.log(data3);
                                        if (data3.code == 200) {
                                            $('table tbody').html('');
                                            if (data3.data.length > 0) {
                                                var finishlist3 = [];
                                                data3.data = data3.data.sort(compare("docTime"));
                                                for(var k = 0; k < data3.data.length; k++) {
                                                    finishlist3.unshift(data3.data[k]);
                                                }
                                                console.log(finishlist3);
                                                for(var i = 0; i < finishlist3.length; i++) {
                                                    $('table tbody').append('<tr><td>' + finishlist3[i].name + '</td><td>' + finishlist3[i].serverName + '</td>' +
                                                        '<td>' + finishlist3[i].serTime + '</td><td>已处理</td><td>' + (msg.userrole==1?finishlist3[i].docTime:finishlist3[i].assTime) + '</td>' +
                                                        '<td><button class="btn detailbtn" listid="' + finishlist3[i]._id + '" status="1">详情</button></td></tr>');
                                                }
                                                $('.detailbtn').on('click', function(e) {
                                                    console.log(1111);
                                                    self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&'+arr[1]+'=' + $(e.target).attr('status');
                                                });
                                            }
                                        }
                                    }
                                });

                            }else{
                                $(e.target).parent().addClass('active');
                                $(e.target).parent().siblings().removeClass('active');
                                $.ajax({
                                    url: url + '/server/assDocFinish',
                                    type: 'post',
                                    data: {
                                        id: arr[1],
                                        userRole: 6,
                                        limit:10,
                                        page:$('.pagination li.active a').text()
                                    },
                                    headers: {
                                        'Authorization': 'Bearer ' + msg.token
                                    },
                                    success: function (data4) {
                                        console.log(data4);
                                        if (data.code == 200) {
                                            $('table tbody').html('');
                                            if (data4.data.length > 0) {
                                                var finishlist4 = [];
                                                data4.data = data4.data.sort(compare("docTime"));
                                                for(var k = 0; k < data4.data.length; k++) {
                                                    finishlist4.unshift(data4.data[k]);
                                                }
                                                console.log(finishlist4);
                                                for(var i = 0; i < finishlist4.length; i++) {
                                                    $('table tbody').append('<tr><td>' + finishlist4[i].name + '</td><td>' + finishlist4[i].serverName + '</td>' +
                                                        '<td>' + finishlist4[i].serTime + '</td><td>已处理</td><td>' + (msg.userrole==1?finishlist4[i].docTime:finishlist4[i].assTime) + '</td>' +
                                                        '<td><button class="btn detailbtn" listid="' + finishlist4[i]._id + '" status="1">详情</button></td></tr>');
                                                }
                                                $('.detailbtn').on('click', function(e) {
                                                    console.log(1111);
                                                    self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&'+arr[1]+'=' + $(e.target).attr('status');
                                                });
                                            }
                                        }
                                    }
                                });
                            }                    })

                        $('.detailbtn').on('click', function(e) {
                            console.log(1111);
                            self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&'+arr[1]+'=' + $(e.target).attr('status');
                        });

                    } else {
                        $('table tbody').append('<tr><td colspan="6"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></td></tr>');
                    }
                } else {
                    alert(data.mes)
                }
            }
        });
    }else{
        $.ajax({
            url: url + '/server/assDocFinish',
            type: 'post',
            data: {
                id: msg.userid,
                userRole: msg.userrole,
                limit:10,
                page:1
            },
            headers: {
                'Authorization': 'Bearer ' + msg.token
            },
            success: function(data) {
                console.log(data);
                if(data.code == 200) {
                    $('table tbody').html('');
                    if(data.data.length > 0) {
                        var n=Math.ceil(data.count/10);
                        if(n>5){
                            var html='<li><a href="#" aria-label="Previous"><span aria-hidden="true">上一页</span></a></li>';
                            html+='<li class="active"><a href="#">1</a></li>' +
                                '<li><a href="#">2</a></li><li><a href="#">3</a></li>' +
                                '<li><a href="#">4</a></li><li><a href="#">5</a></li>';
                            html+='<li><a href="#" aria-label="Next"><span aria-hidden="true">下一页</span></a></li>';
                            $('.pagination').html(html);
                        }else{
                            var html='';
                            for(var a=0;a<n;a++){
                                if(a==0){
                                    html+='<li class="active"><a href="#">'+(a+1)+'</a></li>'
                                }else{
                                    html+='<li><a href="#">'+(a+1)+'</a></li>'
                                }

                            }
                            html+='';
                            $('.pagination').html(html);
                        }
                        var finishlist = [];
                        data.data = data.data.sort(compare("docTime"));
                        for(var k = 0; k < data.data.length; k++) {
                            finishlist.unshift(data.data[k]);
                        }
                        console.log(finishlist);
                        for(var i = 0; i < finishlist.length; i++) {
                            $('table tbody').append('<tr><td>' + finishlist[i].name + '</td><td>' + finishlist[i].serverName + '</td>' +
                                '<td>' + finishlist[i].serTime + '</td><td>已处理</td><td>' +(msg.userrole==1?finishlist[i].docTime:finishlist[i].assTime) + '</td>' +
                                '<td><button class="btn detailbtn" listid="' + finishlist[i]._id + '" status="1">详情</button></td></tr>');
                        }
                        $('.pagination a').on('click',function (e) {
                            console.log($(e.target).text());
                            if($(e.target).text()=='下一页'){
                                if($('.pagination li.active a').text()==n ){
                                    alert('已经到头了');
                                }else if(parseInt($('.pagination li:nth-child(6) a').text())==n){
                                    $('li.active').next().addClass('active');
                                    $($('li.active')[1]).prev().removeClass('active');
                                }else{
                                    $('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text())+1);
                                    $('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text())+1);
                                    $('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text())+1);
                                    $('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text())+1);
                                    $('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text())+1);
                                }
                                $.ajax({
                                    url: url + '/server/assDocFinish',
                                    type: 'post',
                                    data: {
                                        id: msg.userid,
                                        userRole: msg.userrole,
                                        limit:10,
                                        page:$('.pagination li.active a').text()
                                    },
                                    headers: {
                                        'Authorization': 'Bearer ' + msg.token
                                    },
                                    success: function (data2) {
                                        console.log(data2);
                                        if (data2.code == 200) {
                                            $('table tbody').html('');
                                            if (data2.data.length > 0) {
                                                var finishlist2 = [];
                                                data2.data = data2.data.sort(compare("docTime"));
                                                for(var k = 0; k < data2.data.length; k++) {
                                                    finishlist2.unshift(data2.data[k]);
                                                }
                                                console.log(finishlist2);
                                                for(var i = 0; i < finishlist2.length; i++) {
                                                    $('table tbody').append('<tr><td>' + finishlist2[i].name + '</td><td>' + finishlist2[i].serverName + '</td>' +
                                                        '<td>' + finishlist2[i].serTime + '</td><td>已处理</td><td>' + (msg.userrole==1?finishlist2[i].docTime:finishlist2[i].assTime) + '</td>' +
                                                        '<td><button class="btn detailbtn" listid="' + finishlist2[i]._id + '" status="1">详情</button></td></tr>');
                                                }
                                                $('.detailbtn').on('click', function(e) {
                                                    console.log(1111);
                                                    self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&type=' + $(e.target).attr('status');
                                                });
                                            }
                                        }
                                    }
                                });
                            }else if($(e.target).text()=='上一页'){
                                if($('.pagination li.active a').text()==1 ){
                                    alert('已经到头了');
                                }else if(parseInt($('.pagination li:nth-child(2) a').text())==1){
                                    $('li.active').prev().addClass('active');
                                    $($('li.active')[0]).next().removeClass('active');
                                }else{
                                    $('.pagination li:nth-child(6) a').text(parseInt($('.pagination li:nth-child(6) a').text())-1);
                                    $('.pagination li:nth-child(5) a').text(parseInt($('.pagination li:nth-child(5) a').text())-1);
                                    $('.pagination li:nth-child(4) a').text(parseInt($('.pagination li:nth-child(4) a').text())-1);
                                    $('.pagination li:nth-child(3) a').text(parseInt($('.pagination li:nth-child(3) a').text())-1);
                                    $('.pagination li:nth-child(2) a').text(parseInt($('.pagination li:nth-child(2) a').text())-1);
                                }
                                $.ajax({
                                    url: url + '/server/assDocFinish',
                                    type: 'post',
                                    data: {
                                        id: msg.userid,
                                        userRole: msg.userrole,
                                        limit:10,
                                        page:$('.pagination li.active a').text()
                                    },
                                    headers: {
                                        'Authorization': 'Bearer ' + msg.token
                                    },
                                    success: function (data3) {
                                        console.log(data3);
                                        if (data3.code == 200) {
                                            $('table tbody').html('');
                                            if (data3.data.length > 0) {
                                                var finishlist3 = [];
                                                data3.data = data3.data.sort(compare("docTime"));
                                                for(var k = 0; k < data3.data.length; k++) {
                                                    finishlist3.unshift(data3.data[k]);
                                                }
                                                console.log(finishlist3);
                                                for(var i = 0; i < finishlist3.length; i++) {
                                                    $('table tbody').append('<tr><td>' + finishlist3[i].name + '</td><td>' + finishlist3[i].serverName + '</td>' +
                                                        '<td>' + finishlist3[i].serTime + '</td><td>已处理</td><td>' + (msg.userrole==1?finishlist3[i].docTime:finishlist3[i].assTime) + '</td>' +
                                                        '<td><button class="btn detailbtn" listid="' + finishlist3[i]._id + '" status="1">详情</button></td></tr>');
                                                }
                                                $('.detailbtn').on('click', function(e) {
                                                    console.log(1111);
                                                    self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&type=' + $(e.target).attr('status');
                                                });
                                            }
                                        }
                                    }
                                });

                            }else{
                                $(e.target).parent().addClass('active');
                                $(e.target).parent().siblings().removeClass('active');
                                $.ajax({
                                    url: url + '/server/assDocFinish',
                                    type: 'post',
                                    data: {
                                        id: msg.userid,
                                        userRole: msg.userrole,
                                        limit:10,
                                        page:$('.pagination li.active a').text()
                                    },
                                    headers: {
                                        'Authorization': 'Bearer ' + msg.token
                                    },
                                    success: function (data4) {
                                        console.log(data4);
                                        if (data.code == 200) {
                                            $('table tbody').html('');
                                            if (data4.data.length > 0) {
                                                var finishlist4 = [];
                                                data4.data = data4.data.sort(compare("docTime"));
                                                for(var k = 0; k < data4.data.length; k++) {
                                                    finishlist4.unshift(data4.data[k]);
                                                }
                                                console.log(finishlist4);
                                                for(var i = 0; i < finishlist4.length; i++) {
                                                    $('table tbody').append('<tr><td>' + finishlist4[i].name + '</td><td>' + finishlist4[i].serverName + '</td>' +
                                                        '<td>' + finishlist4[i].serTime + '</td><td>已处理</td><td>' + (msg.userrole==1?finishlist4[i].docTime:finishlist4[i].assTime) + '</td>' +
                                                        '<td><button class="btn detailbtn" listid="' + finishlist4[i]._id + '" status="1">详情</button></td></tr>');
                                                }
                                                $('.detailbtn').on('click', function(e) {
                                                    console.log(1111);
                                                    self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&type=' + $(e.target).attr('status');
                                                });
                                            }
                                        }
                                    }
                                });
                            }                    })

                        $('.detailbtn').on('click', function(e) {
                            console.log(1111);
                            self.location = '../../postpaper/postpaper.html?detailId=' + $(e.target).attr('listid') + '&&type=' + $(e.target).attr('status');
                        });

                    } else {
                        $('table tbody').append('<tr><td colspan="6"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></td></tr>');
                    }
                } else {
                    alert(data.mes)
                }
            }
        });
    }
	socket.on('inviteroom', function(data) {
		console.log(data);
		window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
			parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) + 1;
		window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
        $.ajax({
            url: url + '/service/inviteroom',
            type: 'post',
            data: {
                id: msg.userid,
                roomID: data.roomID,
                time: data.time,
                username: data.username,
                state: 0,
                socket_id: data.socket_id,
                roomname: data.roomname
            },
            headers: {
                'Authorization': 'Bearer ' + msg.token
            },
            success: function (d) {
            }
        })
	})
    socket.on('private_msg', function(data) {
        console.log(data);
        window.top.document.getElementsByClassName('unfinish')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) + 1;
        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
    })
	socket.on('inviteroomss', function(data) {
		console.log(data);
		if(data.content == false) {
			window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
				parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
			if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
				window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
			} else {
				window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
			}
		}
	});
	socket.on('assigndoctor', function(data) {
		console.log(data);
		$.ajax({
			url: url + '/server/assDocPending',
			type: 'post',
			data: {
				id: msg.userid,
				userRole: msg.userrole
			},
            headers: {
                'Authorization': 'Bearer ' + msg.token
            },
			success: function(data) {
				console.log(data);
				if(data.code == 200) {
					window.top.document.getElementsByClassName('unfinish')[0].innerHTML = data.data.length;
					// $('.unfinish').text(data.data.length)
					if(data.data.length > 0) {
						window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
					} else {
						window.top.document.getElementsByClassName('unfinish')[0].style.display = 'none';
					}
				} else {
					alert(data.mes)
				}
			}
		});
	})
})
//审核不通过通知消息
socket.on("notthrough_msg",function(data){
    console.log(data.msg+data._id[0]);
    var bottom=-200;
    var timer=setInterval(function () {
        bottom+=4;
        window.top.document.getElementById('msgnew').style.bottom=bottom+'px';
        window.top.document.getElementsByClassName('newmsg')[0].innerHTML='审核不通过';
        if(bottom>0){
            clearInterval(timer);
        }
    },47)
    window.top.document.getElementsByClassName('unfinish')[0].innerHTML=
        parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML)+1;
    window.top.document.getElementsByClassName('unfinish')[0].style.display='inline-block';
})
//撤回指派
socket.on("Undo", function(data2) {
    console.log(data2, 222)
    window.top.document.getElementsByClassName('unfinish')[0].innerHTML =
        parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) - 1;
    if(parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) > 0) {
        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
    } else {
        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'none';
    }
    alert(data2.name+"撤回指派")
})
//撤回邀请
socket.on("UndoInvite", function(data) {
            window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
                parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
            if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
                window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
            } else {
                window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
            }
});

function compare(prop) {
	return function(obj1, obj2) {
		var val1 = obj1[prop];
		var val2 = obj2[prop];
		if(val1 < val2) {
			return -1;
		} else if(val1 > val2) {
			return 1;
		} else {
			return 0;
		}
	}
}